//
//  TYZUploadRequest.m
//  wd-serviceapp
//
//  Created by 孙飞翔 on 2023/7/14.
//

#import "TYZUploadRequest.h"
#import <AFNetworking.h>

@interface AFHttpClients : AFHTTPSessionManager

+ (instancetype)sharedClient;

@end

static const NSTimeInterval RequestTimeoutInterval = 20;

@implementation AFHttpClients

+ (instancetype)sharedClient {
    
    static AFHttpClients *client = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        
        client = [[AFHttpClients alloc] initWithSessionConfiguration:configuration];
        
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", @"text/gif", nil];
        
        client.requestSerializer.timeoutInterval = RequestTimeoutInterval;
        
        client.securityPolicy = [AFSecurityPolicy defaultPolicy];
        
    });
    
    return client;
}

@end


@implementation TYZUploadRequest

/*
 上传图片
 */
//+ (void)uploadImageWithUrl:(NSString *)url params:(nullable NSDictionary *)params name:(NSString *)name fileName:(NSString *)fileName image:(UIImage *)image success:(nullable HttpSuccessDictionary)success failure:(nullable HttpFailureBlock)failure progress:(nullable HttpUploadProgressBlock)progress {
//    
//    [MBProgressHUD showIconMessage:@"" ToView:nil RemainTime:60];
//    
//    NSData *data = UIImagePNGRepresentation(image);
//    
//    [[AFHttpClients sharedClient] POST:url parameters:params headers:@{@"token":[AppContext getInstance].currentUserInfoModel.token} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        
//        [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpg"];
//        
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//        if (progress) {
//            
//            progress(uploadProgress.fractionCompleted);
//            
//        }
//                
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        [MBProgressHUD hideHUDForView:nil];
//        
//        if (success) {
//
//            success(responseObject);
//            
//        }
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        [MBProgressHUD hideHUDForView:nil];
//        
//        [MBProgressHUD showAutoMessage:error.localizedDescription];
//        
//        if (failure) {
//            
//            failure(error);
//            
//        }
//        
//    }];
//}
@end
